package cn.demomaster.quickui.view.banner;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Interpolator;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import cn.demomaster.quickui.R;

/**
 * 广告轮播指示器
 */
public class BannerCursorView2 extends View implements Banner.BannerIndicator {
    public BannerCursorView2(Context context) {
        super(context);
        init();
    }

    public BannerCursorView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BannerCursorView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BannerCursorView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    int normalColor = Color.WHITE;
    int selectedColor = Color.WHITE;

    public void setColor(int normalColor,int selectedColor) {
        this.normalColor = normalColor;
        this.selectedColor = selectedColor;
        postInvalidate();
    }

    private void init() {
        mPaint = new Paint();
        normalColor = getResources().getColor(com.demomaster.quickcompat.R.color.transparent_light_66);
        selectedColor = Color.WHITE;
    }

    float radius = 7;
    float radius_actived = 7;
    float line_width = 30;
    float line_interval = 20;//间隔
    Paint mPaint;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float x1 = getMeasuredWidth() / 2f - ((indicatorCount) * line_width+ Math.max(0,indicatorCount-1) *line_interval)/2f;
        for (int i = 0; i < indicatorCount; i++) {
            float x = (line_width+line_interval) * i;
            float y = getHeight() / 2f - line_width / 4;
            if (tabIndex == i) {
                mPaint.setColor(selectedColor);
            } else {
                mPaint.setColor(normalColor);
            }

            RectF rectF = new RectF(x1 + x, y, x1 + x + line_width, y + line_width / 4);
            canvas.drawRoundRect(rectF, 0, 0, mPaint);
        }
    }

    int indicatorCount;
    public void setLine_width(float line_width) {
        this.line_width = line_width;
        postInvalidate();
    }

    /**
     * 设置间隔
     * @param line_interval
     */
    public void setLine_interval(float line_interval) {
        this.line_interval = line_interval;
        postInvalidate();
    }

    @Override
    public void selecte(int position) {
        tabIndex = position;
        startTransPositonAnimation(currentX, getWidth() / (indicatorCount + 1) * position);
    }

    public void setIndicatorCount(int indicatorCount) {
        this.indicatorCount = indicatorCount;
        postInvalidate();
    }

    int tabIndex;//当前所属index
    int currentX;
    ValueAnimator animator;
    private final Interpolator mInterpolator = new FastOutSlowInInterpolator();

    private void startTransPositonAnimation(int current, int target) {
        animator = ValueAnimator.ofInt(current, target);
        animator.setInterpolator(mInterpolator);
        int mDuration = 300;
        animator.setDuration(mDuration).start();
        animator.addUpdateListener(animation -> {
            currentX = (int) animation.getAnimatedValue();
            invalidate();
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

            }
        });
    }
}
